package org.edu.modules.sxjy.controller;


import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.Objects;
import org.edu.common.api.vo.Result;
import org.edu.common.aspect.annotation.AutoLog;
import org.edu.common.system.base.controller.BaseController;
import org.edu.modules.sxjy.common.noresubmit.CacheLock;
import org.edu.modules.sxjy.entity.VeApply;
import org.edu.modules.sxjy.entity.VeCommon;
import org.edu.modules.sxjy.service.CacheKeyGenerator;
import org.edu.modules.sxjy.service.IVeApplyService;
import org.edu.modules.sxjy.service.impl.CacheKeyGeneratorImp;
import org.edu.modules.sxjy.utils.PageResponseResult;
import org.edu.modules.sxjy.vo.VeApplyAddVo;
import org.edu.modules.sxjy.vo.VeApplyEditVo;
import org.edu.modules.sxjy.vo.VeApplyInfoVo;
import org.edu.modules.sxjy.vo.VeApplyVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author leidq
 * @create 2022-04-08 10:18
 */

@Api(tags={"实习申请审核"})
@RestController
@RequestMapping("/sxjy/veApply")
@Component
@ApiSort(60)
public class VeApplyController
        extends BaseController<VeApply, IVeApplyService>
{
    private static final Logger log = LoggerFactory.getLogger(VeApplyController.class);
    @Autowired
    private IVeApplyService veApplyService;

    @Bean
    public CacheKeyGenerator cacheKeyGenerator()
    {
        return new CacheKeyGeneratorImp();
    }

    @AutoLog("实习申请审核-查询所有记录")
    @ApiOperation(value="查询所有记录", notes="实习申请审核-查询所有记录")
    @GetMapping({"/selectAll"})
    public PageResponseResult selectAll(VeApplyVo veApplyVo, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo, @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, VeCommon veCommon)
    {
        Integer startLine = Integer.valueOf((pageNo.intValue() - 1) * pageSize.intValue());
        List<VeApplyVo> veApplyVoList = this.veApplyService.selectAll(veApplyVo, startLine, pageSize, veCommon);
        int total = this.veApplyService.getSumPage(veApplyVo, veCommon);
        if ((Objects.isNull(veApplyVoList)) && (veApplyVoList.size() <= 0)) {
            return new PageResponseResult(Integer.valueOf(1), 1, 0, Result.OK());
        }
        double totals = total;
        double pageSizes = pageSize.intValue();
        double totalPage = Math.ceil(totals / pageSizes);
        return new PageResponseResult(pageNo, (int)totalPage, total, Result.OK(veApplyVoList));
    }

    @AutoLog("实习申请审核-查看申请")
    @ApiOperation(value="通过id查询详细信息", notes="实习申请审核-查看申请")
    @GetMapping({"/selectOneInfo"})
    public Result<?> selectOneInfo(int id)
    {
        VeApplyInfoVo veApplyInfoVo = this.veApplyService.getOneInfo(id);
        if (veApplyInfoVo == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(veApplyInfoVo);
    }

    @AutoLog("实习申请审核-批量同意通过")
    @ApiOperation(value="批量同意通过", notes="实习申请审核-批量同意通过")
    @PutMapping({"/updatePass"})
    @CacheLock(prefix="redisLock.updatePass", expire=1)
    public Result<?> updatePass(@RequestBody List<VeApply> veApplyList)
    {
        if (veApplyList.size() > 0)
        {
            this.veApplyService.updatePass(veApplyList);
            return Result.OK("批量同意成功");
        }
        return Result.error("批量同意失败");
    }

    @AutoLog("实习申请审核-批量不同意通过")
    @ApiOperation(value="批量不同意通过", notes="实习申请审核-批量不同意通过")
    @PutMapping({"/updateNoPass"})
    @CacheLock(prefix="redisLock.updateNoPass", expire=1)
    public Result<?> updateNoPass(@RequestBody List<VeApply> veApplyList)
    {
        if (veApplyList.size() > 0)
        {
            this.veApplyService.updateNoPass(veApplyList);
            return Result.OK("批量不同意成功");
        }
        return Result.error("批量不同意失败");
    }

    @AutoLog("实习申请审核-实习申请编辑回显")
    @ApiOperation(value="实习申请编辑回显", notes="实习申请审核-实习申请编辑回显")
    @GetMapping({"/udpateEcho"})
    public Result<?> updateEcho(int id)
    {
        if (id != 0)
        {
            VeApply veApply = (VeApply)this.veApplyService.getById(Integer.valueOf(id));
            if (veApply.getType() == 1)
            {
                VeApplyEditVo veApplyEditVo = this.veApplyService.focusEcho(veApply);
                return Result.OK(veApplyEditVo);
            }
            if (veApply.getType() == 2)
            {
                VeApplyEditVo veApplyEditVo = this.veApplyService.disperseEcho(veApply);
                if (veApplyEditVo != null) {
                    return Result.ok(veApplyEditVo);
                }
            }
        }
        return Result.error("编辑回显失败");
    }

    @AutoLog("实习申请审核-集中实习申请编辑")
    @ApiOperation(value="集中实习申请编辑", notes="实习申请审核-集中实习申请编辑")
    @PutMapping({"/focusEdit"})
    @CacheLock(prefix="redisLock.focusEdit", expire=1)
    public Result<?> focusEdit(@RequestBody VeApplyEditVo veApplyEditVo)
    {
        if (veApplyEditVo != null)
        {
            this.veApplyService.focusEdit(veApplyEditVo);
            return Result.OK("编辑成功！");
        }
        return Result.error("编辑失败!");
    }

    @AutoLog("实习申请审核-分散实习申请编辑")
    @ApiOperation(value="分散实习申请编辑", notes="实习申请审核-分散实习申请编辑")
    @PutMapping({"/disperseEdit"})
    @CacheLock(prefix="redisLock.disperseEdit", expire=1)
    public Result<?> disperseEdit(@RequestBody VeApplyEditVo veApplyEditVo)
    {
        if (veApplyEditVo != null)
        {
            this.veApplyService.disperseEdit(veApplyEditVo);
            return Result.OK("编辑成功！");
        }
        return Result.error("编辑失败!");
    }

    @AutoLog("实习申请审核-集中实习申请")
    @ApiOperation(value="集中实习申请", notes="实习申请审核-集中实习申请")
    @PostMapping({"/addFocus"})
    @CacheLock(prefix="redisLock.focusEdit", expire=1)
    public Result<?> addFocus(@RequestBody VeApplyAddVo veApplyAddVo)
    {
        List<VeApply> list = this.veApplyService.list();
        for (VeApply apply : list) {
            if (apply.getStuUserId() == veApplyAddVo.getStuId()) {
                return Result.error("每个学生只能提交一次申请");
            }
        }
        return this.veApplyService.addFocus(veApplyAddVo);
    }

    @AutoLog("实习申请审核-分散实习申请")
    @ApiOperation(value="分散实习申请", notes="实习申请审核-分散实习申请")
    @PostMapping({"/addDis"})
    public Result<?> addDis(@RequestBody VeApplyAddVo veApplyAddVo)
    {
        List<VeApply> list = this.veApplyService.list();
        for (VeApply apply : list) {
            if (apply.getStuUserId() == veApplyAddVo.getStuId()) {
                return Result.error("每个学生只能提交一次申请");
            }
        }
        return this.veApplyService.addDis(veApplyAddVo);
    }

    @AutoLog("实习申请审核-根据企业获得所属岗位")
    @ApiOperation(value="根据企业获得所属岗位", notes="实习申请审核-根据企业获得所属岗位")
    @GetMapping({"/getCpToPostId"})
    public Result<?> getCpToPostId(int id)
    {
        if (id != 0)
        {
            List<Integer> postId = this.veApplyService.getCpToPostId(id);
            return Result.ok(postId);
        }
        return Result.error("企业不存在");
    }

    @AutoLog("实习申请审核-查看学生当前的状态是否可申请实习")
    @ApiOperation(value="查看学生当前的状态是否可申请实习", notes="实习申请审核-查看学生当前的状态是否可申请实习")
    @GetMapping({"/stuIsStatusApply"})
    public Result<?> stuIsStatusApply(String userType, String userId)
    {
        return this.veApplyService.stuIsStatusApply(userType, userId);
    }

    @AutoLog("实习申请审核-通过id删除")
    @ApiOperation(value="通过id删除", notes="实习申请审核-通过id删除")
    @DeleteMapping({"/delete"})
    public Result<?> deleteById(@RequestParam(name="id", required=true) int id)
    {
        return this.veApplyService.deleteById(id);
    }

    @AutoLog("实习申请审核-批量删除")
    @ApiOperation(value="批量删除", notes="实习申请审核-批量删除")
    @DeleteMapping({"/deleteBatch"})
    public Result<?> deleteBatch(@RequestParam(name="ids", required=true) String ids)
    {
        return this.veApplyService.deleteBatch(ids);
    }
}

